// import Header from '@/components/LayoutHeader';
import { connect, history } from '@umijs/max';
// import { Button } from 'antd';
import { toNumber } from 'lodash';
import { nanoid } from 'nanoid';
import React, { useEffect, useRef, useState } from 'react';
// import { KeepAlive } from 'umi';
import Doc from './com/doc';
import History from './com/history';
import './index.less';

const ChatBot: React.FC = (props) => {
  // const [title, setTitle] = useState<string>('');
  // const [isAdmin, setIsAdmin] = useState<boolean>(false);
  // const { initialState } = useModel('@@initialState');
  //左侧点击的会话id
  const [conversationID, setConversationID] = useState<number>();
  const [knowledgeId, setKnowledgeId] = useState<number>(-1);
  const title = useRef<string>('');

  // const dataDoc = (id: any) => {
  //   history.push(`/dataQuestion?kbid=${id}`);
  // };
  useEffect(() => {
    const query = history.location.search;
    const type = toNumber(query.split('?')[1].split('=')[1]);
    if (type === 5) {
      title.current = '电力行业数字化知识小助手';
      setKnowledgeId(5);
    } else if (type === 6) {
      title.current = '商旅云客服FAQ';
      setKnowledgeId(6);
    } else if (type === 8) {
      title.current = '营销服务中心客服FAQ';
      setKnowledgeId(8);
    } else if (type === 9) {
      title.current = '自由写作';
      setKnowledgeId(9);
    }
    //修改headerModel的title
    props.dispatch({
      type: 'headerModel/changeTitle',
      payload: title.current,
    });
    // if (initialState?.currentUser?.role === 'admin') {
    //   //如果是admin 顶部显示按钮
    //   props.dispatch({
    //     type: 'headerModel/updateRightCon',
    //     payload: (
    //       <Button
    //         className="dataBtn"
    //         onClick={() => {
    //           dataDoc(type);
    //         }}
    //       >
    //         前往知识库
    //       </Button>
    //     ),
    //   });
    // }
  }, []);

  const changeID = (id: number) => {
    setConversationID(id);
  };

  //是否是新会话
  const [isCreateNew, setIsCreateNew] = useState<boolean>(false);
  const changeNew = (isN: boolean) => {
    setIsCreateNew(isN);
  };
  //用来刷新历史列表
  const [historyRefresh, setHistoryRefresh] = useState<string>('');
  const changeHistoryList = () => {
    setHistoryRefresh(nanoid());
  };
  //用来检查删除的 是否是当前会话 如果是清空会话界面
  const [isDelCur, setIsDelCur] = useState<string>('');
  const changeDelCur = (id) => {
    setIsDelCur(id);
  };

  return (
    <div className="page-container">
      {/* <Header title={title} showDataBtn={isAdmin} knowledgeId={knowledgeId}></Header> */}
      {/* <KeepAlive id={knowledgeId}>

      </KeepAlive> */}
      <div className="container-chat">
        {/* 历史列表 */}
        <div className="refer-chat">
          <History
            changeId={changeID}
            changeNew={changeNew}
            knowledgeId={knowledgeId}
            historyRefresh={historyRefresh}
            changeDelCur={changeDelCur}
          />
        </div>
        {/* 右边布局，对话框 */}
        <div className="content-chat">
          <Doc
            id={conversationID}
            changeNew={changeNew}
            knowledgeId={knowledgeId}
            isCreateNew={isCreateNew}
            changeHistoryList={changeHistoryList}
            isDelCur={isDelCur}
          />
          {/* <div className="doc-content">2</div>
          <div className='doc-question'>3</div> */}
        </div>
      </div>
    </div>
  );
};

// export default ChatBot;
export default connect(({ headerModel }) => ({ headerModel }))(ChatBot);
